//
//  HRMainNavigtionController.swift
//  microBlog
//
//  Created by HR_W on 16/5/20.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

class HRMainNavigtionController: UINavigationController ,UIGestureRecognizerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        //解决右滑返回手势的问题
        //存在bug 在最顶级的页面 右滑返回手势任然响应
        //遵守手势协议
        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    //重写push 方法
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //判断是否是顶级页面
        var backTitle = "返回"
        if childViewControllers.count > 0 {
            
            if childViewControllers.count == 1 {
                //设置返回按钮的title = 顶级页面的title
                backTitle = childViewControllers.first?.title ?? "返回"
            }
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", target: self, action: #selector(popToFontVc), title: backTitle)
            viewController.navigationItem.leftBarButtonItem = back
          
            
        }
        
        super.pushViewController(viewController, animated: animated)
        //print(childViewControllers.count)
    }
    
    @objc private func popToFontVc() {
    
        
        popViewControllerAnimated(true)
    }
    //实现手势协议方法
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
//        if childViewControllers.count > 1 {
//            return true
//        }
//        return false
        
        //简写上面的,当进入子界面才有手势方法
        return childViewControllers.count > 1
    }
    
}
